<template>
  <div>
    <div class="container">
            <p>通过指令 v-dialogDrag 使 Dialog 对话框具有可拖拽的功能。</p>
            <br>
            <el-button type="primary" @click="visible = true;">点我弹框</el-button>
    </div>
    <el-dialog  title="拖拽弹框" center :visible.sync="visible" width="30%" >
            我是一个可以拖拽的对话框！
            <span slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button type="primary" @click="visible = false">确 定</el-button>
            </span>
        </el-dialog>

    <div class="bigBox">
        <div
        ref="customerService"
        class="customer-service"
        @touchstart="touchstartHandle('customerService', $event)"
        @touchmove="touchmoveHandle('customerService', $event)"
      >
        <!-- <span class="iconfont">123</span> -->
        <table border="1" cellspacing="0" width="100px">
          <tr>
            <td>12</td>
            <td>2</td>
            <td>3</td>
          </tr>
          <tr>
            <td>12</td>
            <td>2</td>
            <td>3</td>
          </tr>
        </table>
      </div>
      <div class="boxRight">
        1
      </div>
    </div>
    <!-- <router-view /> -->
    <div class="bigBox2">大盒子</div>
  </div>
</template>

<script>
export default {
  name: "",
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      touch: {
        initialPosition: {},
        movePostion: {},
      },
      element: {
        initialPosition: {},
        movePostion: {},
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    touchstartHandle(refName, e) {
      console.log(e)
      // 触摸开始
      let touchTarget = e.targetTouches[0];
      // 记录触摸点的坐标（初始位置）
      this.touch.initialPosition = {
        x: touchTarget.clientX,
        y: touchTarget.clientY,
      };
      // 记录需要移动的元素坐标（初始位置）
      this.element.initialPosition = {
        x: this.$refs[refName].offsetLeft,
        y: this.$refs[refName].offsetTop,
      };
    },
    touchmoveHandle(refName, e) {
      e.preventDefault();
      let touchTarget = e.targetTouches[0];
      // 根据初始touch位置计算移动距离:元素移动位置=元素初始位置+(光标移动后的位置-光标点击时的初始位置)
      let X =
        this.element.initialPosition.x +
        (touchTarget.clientX - this.touch.initialPosition.x);
      let Y =
        this.element.initialPosition.y +
        (touchTarget.clientY - this.touch.initialPosition.y);
      // 限制可移动距离，不超出可视区域
      let maxWidth = innerWidth - this.$refs[refName].offsetWidth;
      let maxHeight = innerHeight - this.$refs[refName].offsetHeight;
      X = X <= 0 ? 0 : X >= maxWidth ? maxWidth : X;
      Y = Y <= 0 ? 0 : Y >= maxHeight ? maxHeight : Y;
      // console.log(X,Y)

      // 移动元素
      this.$refs[refName].style.left = X + "px";
      this.$refs[refName].style.top = Y + "px";
    },
  },
};
</script>

<style lang="less" scoped>
// @import "./assets/icon/iconfont.css";
// * {
//   margin: 0;
//   padding: 0;
//   list-style: none;
//   box-sizing: border-box;
// }
// html,
// body {
//   width: 100%;
//   height: 100%;
// }
// #nav {
//   padding: 0;
//   margin: 0;
// }
// #app {
//   position: relative;
// }
.customer-service {
  position: absolute;
  // height: 160px;
  // width: 160px;
  // border-radius: 50%;
  z-index: 999;
  color: #fff;
  font-size: 24px;
  background: rgb(0, 122, 255);
  text-align: center;
  line-height: 60px;
}
.bigBox{
  width: 600px;
  height: 400px;
  background: lightcoral;
  // position: relative;
}
table{
  margin: 0 auto;
}
.bigBox2{
  width: 600px;
  height: 1200px;
  background: yellowgreen;
}
</style>
